<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.    
-->

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:amq="http://activemq.apache.org/schema/core" 
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.5.0.xsd
          http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.1.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="file://conf/jmx.properties"/>
    </bean>

    <bean id="restrictedRmiSocketFactory" class="org.apache.james.container.spring.RestrictingRMISocketFactory">
        <constructor-arg value="${jmx.address}"/>
    </bean>   

    <bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
        <property name="port" value="${jmx.port}" />
        <property name="serverSocketFactory" ref="restrictedRmiSocketFactory"/>
        <property name="clientSocketFactory" ref="restrictedRmiSocketFactory"/>
    </bean> 

    <bean id="mbeanserver" class="org.springframework.jmx.support.MBeanServerFactoryBean" >
        <property name="locateExistingServerIfPossible" value="true"/>
        <property name="registerWithFactory" value="true"/>
    </bean>

    <bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean" depends-on="registry">
        <property name="objectName" value="connector:name=rmi" />
        <property name="serviceUrl" value="service:jmx:rmi://${jmx.address}/jndi/rmi://${jmx.address}:${jmx.port}/jmxrmi" />
    </bean> 

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
      <property name="beans">
        <map>
          <entry key="org.apache.james:type=server,name=smtpserver" value-ref="smtpserver"/>
          <entry key="org.apache.james:type=server,name=lmtpserver" value-ref="lmtpserver"/>
          <entry key="org.apache.james:type=server,name=pop3server" value-ref="pop3server"/>
          <entry key="org.apache.james:type=server,name=imapserver" value-ref="imapserver"/>
          <entry key="org.apache.james:type=server,name=remotemanager" value-ref="remotemanager"/>
          <entry key="org.apache.james:type=component,name=domainlist" value-ref="domainlistmanagement"/>
          <entry key="org.apache.james:type=component,name=dnsservice" value-ref="dnsservice"/>
          <entry key="org.apache.james:type=component,name=processor" value-ref="processormanagement"/>
          <entry key="org.apache.james:type=component,name=virtualusertable" value-ref="virtualusertablemanagement"/>
          <entry key="org.apache.james:type=component,name=usermanagement" value-ref="usermanagement"/>
          <entry key="org.apache.james:type=component,name=fetchmail" value-ref="fetchmail"/>
          <entry key="org.apache.james:type=component,name=mailboxmanagement" value-ref="mailboxmanagermanagement"/>
        </map>
      </property>
      
      <property name="assembler">
          <bean class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
            <property name="managedInterfaces" value="org.apache.james.smtpserver.netty.SMTPServerMBean,org.apache.james.remotemanager.netty.RemoteManagerMBean,org.apache.james.pop3server.netty.POP3ServerMBean,org.apache.james.imapserver.netty.IMAPServerMBean,org.apache.james.fetchmail.FetchSchedulerMBean,org.apache.james.domainlist.api.DomainListManagementMBean,org.apache.james.dnsservice.api.DNSServiceMBean,org.apache.james.mailetcontainer.api.ProcessorManagementMBean,org.apache.james.vut.api.VirtualUserTableManagementMBean,org.apache.james.user.api.UserManagementMBean,org.apache.james.adapter.mailbox.MailboxManagerManagementMBean"/>
          </bean>
      </property>
      
    </bean>
    
    <bean id="processormanagement" class="org.apache.james.mailetcontainer.lib.ProcessorManagement"/>
    
    <bean id="usermanagement" class="org.apache.james.user.lib.UserManagement" />
    
    <bean id="virtualusertablemanagement" class="org.apache.james.vut.lib.VirtualUserTableManagement" />

    <bean id="domainlistmanagement" class="org.apache.james.domainlist.lib.DomainListManagement" />

    <bean id="mailboxmanagermanagement" class="org.apache.james.adapter.mailbox.MailboxManagerManagement" />

    <bean class="org.apache.james.container.spring.lifecycle.CommonsConfigurableBeanPostProcessor">
        <property name="configurationProvider" ref="configurationProvider" />
        <property name="order" value="1" />
    </bean>

    <bean id="configurationProvider" class="org.apache.james.container.spring.lifecycle.SpringConfigurationProvider">
        <property name="configurationMappings">
            <map>
                <entry key="lmtpProtocolHandlerChain" value="lmtpserver"/>
                <entry key="smtpProtocolHandlerChain" value="smtpserver"/>
                <entry key="pop3ProtocolHandlerChain" value="pop3server"/>
                <entry key="remoteProtocolHandlerChain" value="remotemanager"/>
                <entry key="mailProcessor" value="mailetcontainer"/>
                <entry key="spoolmanager" value="mailetcontainer"/>
                <entry key="mailetloader" value="mailetcontainer"/>
                <entry key="matcherloader" value="mailetcontainer"/>
                <entry key="usersstore" value="usersrepository"/>
             </map>
        </property>
    </bean>

    <bean class="org.apache.james.container.spring.FileSystemResolver">
        <property name="fileSystem" ref="filesystem" />
    </bean>
    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name = "location" value="file://conf/database.properties"/>
    </bean>

    <bean class="org.apache.james.container.spring.lifecycle.LogEnabledBeanPostProcessor">
        <property name="logProvider" ref="logProvider" />
        <property name="order" value="0" />
    </bean>

    <bean id="logProvider" class="org.apache.james.container.spring.lifecycle.SpringLogProvider">
        <property name="logMappings">
            <map>
                <entry key="lmtpProtocolHandlerChain" value="lmtpserver"/>
                <entry key="smtpProtocolHandlerChain" value="smtpserver"/>
                <entry key="pop3ProtocolHandlerChain" value="pop3server"/>
                <entry key="remoteProtocolHandlerChain" value="remoteManager"/>
                <entry key="mailProcessor" value="mailetcontainer"/>
                <entry key="spoolmanager" value="mailetcontainer"/>
                <entry key="mailetloader" value="mailetcontainer"/>
                <entry key="matcherloader" value="mailetcontainer"/>
                <entry key="usersstore" value="usersrepository"/>
            </map>
        </property>
    </bean>

    <bean class= "org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
        <property name="order" value="3" />
    </bean>

    <bean id="instanceFactory" class="org.apache.james.container.spring.SpringInstanceFactory"/>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <camel:camelContext id="jamesCamelContext" trace="false" >
        <camel:jmxAgent id="agent" disabled="false" createConnector="false" serviceUrlPath="/jmxrmi/jamescamel"/>
        <camel:template id="producerTemplate"/>   
    </camel:camelContext>

    <amq:broker useJmx="true" persistent="true" brokerName="james" dataDirectory="filesystem=file://var/store/activemq/brokers" useShutdownHook="false" schedulerSupport="false" id="broker">
        <amq:destinationPolicy>
            <amq:policyMap>
                <amq:policyEntries>
                    <!-- Support priority handling of messages -->
                    <!-- http://activemq.apache.org/how-can-i-support-priority-queues.html -->
                    <amq:policyEntry queue=">" prioritizedMessages="true"/>
                </amq:policyEntries>
            </amq:policyMap>
        </amq:destinationPolicy>
        <amq:managementContext>
            <amq:managementContext createConnector="false"/>
        </amq:managementContext>
        <amq:persistenceAdapter>
            <amq:amqPersistenceAdapter/>
        </amq:persistenceAdapter>
        <amq:transportConnectors>
            <amq:transportConnector uri="tcp://localhost:0" />
        </amq:transportConnectors>
    </amq:broker>
    
    <amq:connectionFactory id="amqConnectionFactory" brokerURL="vm://james?create=false">
        <property name="blobTransferPolicy" ref="blobTransferPolicy"/>
    </amq:connectionFactory>

    <bean id="blobTransferPolicy" class="org.apache.james.queue.activemq.FileSystemBlobTransferPolicy">
        <property name="defaultUploadUrl" value="file://var/store/activemq/blob-transfer"/>
    </bean>

    <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
       <property name="targetConnectionFactory" ref="amqConnectionFactory"/>
       <property name="sessionCacheSize" value="10"/>
       <property name="cacheConsumers" value="false"/>
       <property name="cacheProducers" value="true"/>
    </bean>

    <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
    </bean>

    <bean id="mailQueueFactory" class="org.apache.james.queue.activemq.ActiveMQMailQueueFactory" depends-on="broker"/>
        
    <bean id="mailProcessor" name="processorRoute" class="org.apache.james.mailetcontainer.camel.CamelMailProcessorList"/>

    <bean id="spoolmanager" class="org.apache.james.transport.JamesSpoolManager"/>
    
    <bean id="mailserver" class="org.apache.james.JamesMailServer"/>

    <bean id="mailetcontext" class="org.apache.james.mailetcontainer.lib.JamesMailetContext"/>

    <bean id="matcherloader" class="org.apache.james.mailetcontainer.lib.JamesMatcherLoader" />

    <bean id="mailetloader" class="org.apache.james.mailetcontainer.lib.JamesMailetLoader" />

    <bean id="dnsservice" class="org.apache.james.dnsservice.dnsjava.DNSJavaService" />

    <bean id="remotemanager" class="org.apache.james.remotemanager.netty.RemoteManager" >
        <property name="protocolHandlerChain" ref="remoteProtocolHandlerChain"/>
    </bean>

    <bean id="remoteProtocolHandlerChain" class="org.apache.james.socket.JamesProtocolHandlerChain">
        <property name="coreHandlersPackage" value="org.apache.james.remotemanager.core.CoreCmdHandlerLoader"/>
    </bean>

    <bean id="pop3server"  class="org.apache.james.pop3server.netty.POP3Server" >
        <property name="protocolHandlerChain" ref="pop3ProtocolHandlerChain"/>
    </bean>

    <bean id="pop3ProtocolHandlerChain" class="org.apache.james.socket.JamesProtocolHandlerChain">
        <property name="coreHandlersPackage" value="org.apache.james.pop3server.core.CoreCmdHandlerLoader"/>
    </bean>

    <bean id="smtpserver" class="org.apache.james.smtpserver.netty.SMTPServer">
        <property name="protocolHandlerChain" ref="smtpProtocolHandlerChain"/>
    </bean>

    <bean id="smtpProtocolHandlerChain" class="org.apache.james.socket.JamesProtocolHandlerChain">
        <property name="coreHandlersPackage" value="org.apache.james.smtpserver.CoreCmdHandlerLoader"/>
    </bean>

    <bean id="lmtpserver" class="org.apache.james.lmtpserver.netty.LMTPServer">
        <property name="protocolHandlerChain" ref="lmtpProtocolHandlerChain"/>
    </bean>

    <bean id="lmtpProtocolHandlerChain" class="org.apache.james.socket.JamesProtocolHandlerChain">
        <property name="coreHandlersPackage" value="org.apache.james.lmtpserver.CoreCmdHandlerLoader"/>
    </bean>
    
    <bean id="fetchmail" class="org.apache.james.fetchmail.FetchScheduler" />

    <bean id="mailstore" class="org.apache.james.mailstore.lib.JamesMailStore" />

    <bean id="usersstore" class="org.apache.james.user.lib.JamesUsersStore" >
        <property name="defaultRepository" value="LocalUsers"/>
    </bean>

    <bean id="localusersrepository" class="org.apache.james.user.lib.LocalUsersRepository" />

    <bean id="filesystem" class="org.apache.james.container.spring.SpringFileSystem" />

    <bean id="virtualusertable" class="org.apache.james.vut.lib.JamesVirtualUserTable" />

    <bean id="domainlist" class="org.apache.james.domainlist.lib.JamesDomainList" />

    <bean id="imapserver" class="org.apache.james.imapserver.netty.IMAPServer">
        <property name="imapDecoder" ref="imapDecoder"/>
        <property name="imapEncoder" ref="imapEncoder"/>    
    </bean>

    <bean id="imapProcessor" class="org.apache.james.imap.processor.main.DefaultImapProcessorFactory" factory-method="createDefaultProcessor">
        <constructor-arg index="0" ref="mailboxmanager"/>
        <constructor-arg index="1" ref="subscriptionManager"/>
    </bean>

    <bean id="imapDecoderFactory" class="org.apache.james.imap.main.DefaultImapDecoderFactory"/>

    <bean id="imapDecoder" factory-bean="imapDecoderFactory"  factory-method="buildImapDecoder"/>

    <bean id="imapEncoderFactory" class="org.apache.james.imap.encode.main.DefaultImapEncoderFactory"/>

    <bean id="imapEncoder" factory-bean="imapEncoderFactory"  factory-method="buildImapEncoder"/>

    <bean id="authenticator" class="org.apache.james.adapter.mailbox.store.UserRepositoryAuthenticator"/>

    <bean id="sessionMapperFactory" class="org.apache.james.mailbox.jpa.JPAMailboxSessionMapperFactory">
        <constructor-arg index="0" ref="entityManagerFactory"/>
    </bean>
    
    <bean id ="subscriptionManager" class="org.apache.james.mailbox.jpa.JPASubscriptionManager">
        <constructor-arg index="0" ref="sessionMapperFactory"/>
    </bean>

    <bean id="mailboxmanager" class="org.apache.james.mailbox.jpa.openjpa.OpenJPAMailboxManager">
        <constructor-arg index="0" ref="sessionMapperFactory"/>
        <constructor-arg index="1" ref="authenticator"/>
        <constructor-arg index="2" value="${openjpa.streaming}"/>
    </bean>

    <bean id="scheduler" class="java.util.concurrent.Executors" factory-method="newScheduledThreadPool">
        <constructor-arg value="20"/>
    </bean>
    
    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
    </bean>
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <property name="jpaVendorAdapter" ref="vendorAdapter"/>
    </bean>

    <bean id="vendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
        <property name="database" value="${vendorAdapter.database}"/>
        <!-- set this to true for debugging purposes -->
        <property name="showSql" value="false"/>
    </bean>

</beans>
